﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Product;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.BusinessLogic.Common;

namespace OpenSoftStore.Client.UIL.ManageMember
{
    public partial class MemberTreeDinnerAdd : UserControl
    {
        private ProductLogic productBll;
        private MemberLogic memberBll;
        private TreeLogic treeBll;
        private T_Tree Tree;
        private T_MemberTree memberTree;
        private List<T_MemberTreeDinner> memberTreeDinners;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public MemberTreeDinnerAdd()
        {
            InitializeComponent();
            productBll = new ProductLogic();
            memberBll = new MemberLogic();
            treeBll = new TreeLogic();
            Tree = new T_Tree();
            memberTree = new T_MemberTree();
            memberTreeDinners = new List<T_MemberTreeDinner>();
            SPACommon.SetDataGridStyle(dgvProduct);
            SPACommon.SetDataGridStyle(dgvProductItem);
            LoadControlData();
        }

        private void LoadControlData()
        {
            dgvProductItem.AutoGenerateColumns = false;
            dgvProduct.AutoGenerateColumns = false;
            dgvProduct.DataSource = SPACommon.ToDataTable(productBll.GetProdutView());
            lblProductCount.Text = dgvProduct.RowCount.ToString();
        }

        public void GetData(string id)
        {
            Tree.Id = id;
            T_Tree tree = productBll.Where<T_Tree>(t => t.Id == id).FirstOrDefault();
            txtMemberType.Text = tree.Name;
            memberTree = memberBll.Where<T_MemberTree>(m => m.TreeId == id).FirstOrDefault();
            if (memberTree == null)
            {
                memberTree = new T_MemberTree();
                return;
            }
            numDiscount.Value = memberTree.Discount;
            numPresent.Value = memberTree.Present;
            memberTreeDinners = memberBll.GetMemberTreeDinnerView(" where T_MemberTree.TreeId='" + id + "'").ToList();
            if (memberTreeDinners == null)
            {
                memberTreeDinners = new List<T_MemberTreeDinner>();
            }
            dgvProductItem.DataSource = SPACommon.ToDataTable(memberTreeDinners);
        }

        public void SetTree(T_Tree tree)
        {
            Tree = tree;
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.FindForm().Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string typeName = txtMemberType.Text.Trim();
            if (string.IsNullOrEmpty(typeName))
            {
                FormSysMessage.ShowMessage("客户类型不能为空！");
                return;
            }
            if (dgvProductItem.RowCount == 0) return;
            Tree.Name = typeName;
            //if (string.IsNullOrEmpty(memberTree.Id)) memberTree.Id = Guid.NewGuid().ToString("D");
            memberTree.Discount = numDiscount.Value;
            memberTree.Present = numPresent.Value;
            var table = (DataTable)dgvProductItem.DataSource ?? new DataTable();
            var memTrees = SPACommon.ToList<T_MemberTreeDinner>(table);
            memberBll.SaveMemberTreeDinner(Tree, memberTree, memTrees);
            this.FindForm().Close();
            MemberList.Instance.RefreshTree();
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            var table = new DataTable("T_MemberTreeDinner");
            table.Columns.Add("ProductName", typeof(String));
            table.Columns.Add("ProductId", typeof(String));
            for (int i = 0; i < dgvProduct.Rows.Count; i++)
            {
                if (dgvProduct.Rows[i].Cells["IsChecked"].Value == null)
                {
                    continue;
                }
                if (dgvProduct.Rows[i].Cells["IsChecked"].Value.ToString() == "是")
                {
                    var row = table.NewRow();
                    row["ProductName"] = dgvProduct.Rows[i].Cells["name"].Value.ToString();
                    row["ProductId"] = dgvProduct.Rows[i].Cells["productId"].Value.ToString();
                    table.Rows.Add(row);
                }
            }
            dgvProductItem.DataSource = table;
            lblCount.Text = dgvProductItem.RowCount.ToString();
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            if (dgvProductItem.CurrentRow == null) return;
            dgvProductItem.Rows.RemoveAt(dgvProductItem.CurrentRow.Index);
            lblCount.Text = dgvProductItem.RowCount.ToString();

        }

        private void txtProductSearch_KeyUp(object sender, KeyEventArgs e)
        {
            string whereStr = txtProductSearch.Text.Trim();
            if (rbSelect.Checked)
            {
                dgvProduct.DataSource = productBll.GetProdutView(string.Format(" where P.Name like '{0}%' or P.ProductNo like '{0}%' or P.Model like '{0}%' or prov.Name like '{0}%' or T.Code like '{0}%'", whereStr));
            }
            else
            {
                dgvProduct.DataSource = productBll.GetProdutView(" where E.BarCode='" + whereStr + "'");
            }
            lblProductCount.Text = dgvProduct.RowCount.ToString();
        }

        private void txtProductSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (rbBarCode.Checked)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    string whereStr = txtProductSearch.Text;
                    dgvProduct.DataSource = productBll.GetProdutView(" where E.BarCode='" + whereStr + "'");
                    lblProductCount.Text = dgvProduct.RowCount.ToString();
                    btnAddItem_Click(null, null);
                }
            }
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }

        bool flag = false;
        private void btnAll_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < dgvProduct.Rows.Count; i++)
            {
                if (flag == true)
                {
                    dgvProduct.Rows[i].Cells["IsChecked"].Selected = false;
                    dgvProduct.Rows[i].Cells["IsChecked"].Value = "否";
                }
                else
                {
                    dgvProduct.Rows[i].Cells["IsChecked"].Selected = true;
                    dgvProduct.Rows[i].Cells["IsChecked"].Value = "是";
                }

            }
            flag = flag == true ? false : true;
        }
    }
}
